<?php
/**
 * Copyright 2009 Joe Dotoff
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

define('PDOG_LIB', realpath('lib'));

buildCompiler();

require PDOG_LIB . '/Runtime/Compiler.php';

Compiler::compileAll('lib');

function buildCompiler($lib_path = PDOG_LIB)
{
    if (!extension_loaded('bcompiler')) {
        $is_loaded = false;
        try {
            if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
                $is_loaded = dl('php_bcompiler.dll');
            } else {
                $is_loaded = dl('bcompiler.so');
            }
        } catch (Exception $e) {
            $is_loaded = false;
        }
        if (false === $is_loaded) {
            die('Bcompiler extension not loaded');
        }
    }
    $path = PDOG_LIB . '/Runtime/Compiler.php';
    $out = preg_replace('/\.php$/i', '.phc', $path);
    $fp = fopen($out, 'w');
    bcompiler_write_header($fp);
    bcompiler_write_file($fp, $path);
    bcompiler_write_footer($fp);
    fclose($fp);
}
?>